package com.jw.xzg.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jw.xzg.R;
import com.jw.xzg.activity.MainActivity;
import com.jw.xzg.model.DealItem;
import com.jw.xzg.model.NewsItem;
import com.jw.xzg.model.ReportItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/13.
 */

public class DealAdapter extends BaseAdapter {
    public final static Integer TAB_RECORD =1;
    public final static Integer TAB_NEWS = 2;
    public final static Integer TAB_REPORT = 3;
    private Integer currentTab = 1;
    private LayoutInflater mInflater;
    private List<DealItem> dealList = new ArrayList<DealItem>();
    private List<NewsItem> newsList  = new ArrayList<NewsItem>();
    private List<ReportItem> reportList = new ArrayList<ReportItem>();

    public DealAdapter(Context context) {
        this.mInflater = LayoutInflater.from(context);
    }

    public Integer getCurrentTab() {
        return currentTab;
    }

    public void setCurrentTab(Integer currentTab) {
        this.currentTab = currentTab;
    }

    public List<DealItem> getDealList() {
        return dealList;
    }

    public void setDealList(List<DealItem> dealList) {
        this.dealList = dealList;
    }

    public List<NewsItem> getNewsList() {
        return newsList;
    }

    public void setNewsList(List<NewsItem> newsList) {
        this.newsList = newsList;
    }

    public List<ReportItem> getReportList() {
        return reportList;
    }

    public void setReportList(List<ReportItem> reportList) {
        this.reportList = reportList;
    }

    @Override
    public int getCount() {
        if(currentTab.equals(TAB_RECORD)){
            return dealList.size();
        }else if(currentTab.equals(TAB_NEWS)){
            return newsList.size();
        }else if(currentTab.equals(TAB_REPORT)){
            return reportList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(currentTab.equals(TAB_RECORD)){
            return dealList.get(position);
        }else if(currentTab.equals(TAB_NEWS)){
            return newsList.get(position);
        }else if(currentTab.equals(TAB_REPORT)){
            return reportList.get(position);
        }
        return null;

    }

    @Override
    public long getItemId(int position)
    {
        if(currentTab.equals(TAB_RECORD)){
            return dealList.get(position).getId();
        }
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
               if(currentTab.equals(TAB_RECORD)){

                   DealItem item = (DealItem) getItem(position);

                   if((item.getCstatus().equals("1")&&!item.istoFrom())
                           ||(item.getCstatus().equals("2")&&item.istoFrom())
                           ){
                       convertView = mInflater.inflate(R.layout.deal_item_operate_view, null);

                   }else{
                       convertView = mInflater.inflate(R.layout.deal_item_view, null);

                   }
                    DealAdapter.DealViewHolder holder = new DealAdapter.DealViewHolder();
                /*得到各个控件的对象*/
                  holder.fromUserName = (TextView) convertView.findViewById(R.id.deal_item_fromUserName);
                  holder.money = (TextView) convertView.findViewById(R.id.deal_item_money);
                   holder.fromUserName.setText(item.getFromUserName());
                   holder.money.setText(String.valueOf(item.getMoney()));
                   if((item.getCstatus().equals("1")&&!item.istoFrom())
                           ||(item.getCstatus().equals("2")&&item.istoFrom())
                           ){
                       //绑定点击事件
                       convertView.findViewById(R.id.trans_agree).setOnClickListener(new View.OnClickListener() {
                           @Override
                           public void onClick(View v) {
                              DealItem item  =  (DealItem) getItem(position);
                               if(item.istoFrom()){ //sender
                                   MainActivity.finishDeal(item);
                               }else{ //reciver
                                   MainActivity.agreeDeal(item);
                               }
                           }
                       });

                       convertView.findViewById(R.id.trans_refuse).setOnClickListener(new View.OnClickListener() {
                           @Override
                           public void onClick(View v) {
                               DealItem item  =  (DealItem) getItem(position);
                               MainActivity.refuseDeal(item);
                           }
                       });



                   }else if(item.getCstatus().equals("3")){
                       holder.content =(TextView)convertView.findViewById(R.id.deal_item_content);
                       holder.content.setText("交易完成");
                   }else if(item.getCstatus().equals("4")){
                       holder.content =(TextView)convertView.findViewById(R.id.deal_item_content);
                       holder.content.setText("一方拒绝");
                   }else{
                       holder.content =(TextView)convertView.findViewById(R.id.deal_item_content);
                       holder.content.setText("等待对方");
                   }
                  // holder.content.setText(item.getContent());

                   convertView.setTag(holder);
            }else if(currentTab.equals(TAB_NEWS)){
                convertView = mInflater.inflate(R.layout.news_item_view, null);
                   DealAdapter.NewsViewHolder holder = new DealAdapter.NewsViewHolder();

                   holder.title = (TextView) convertView.findViewById(R.id.news_item_title);
                   holder.content = (TextView) convertView.findViewById(R.id.news_item_content);
                   holder.createTime =(TextView)convertView.findViewById(R.id.news_item_createTime);

                   NewsItem item = (NewsItem) getItem(position);
                   holder.title.setText(item.getTitle());
                   holder.content.setText(item.getContent());
                   holder.createTime.setText(item.getCreateTime());

                   convertView.setTag(holder);
               }else if(currentTab.equals(TAB_REPORT)){
                   convertView = mInflater.inflate(R.layout.report_item_view, null);
                   DealAdapter.ReportViewHolder holder = new DealAdapter.ReportViewHolder();
                   holder.decription =  (TextView) convertView.findViewById(R.id.report_description);
                   ReportItem item = (ReportItem) getItem(position);
                   holder.decription.setText(item.getDescription());
                   convertView.setTag(holder);


               }



            //convertView.setTag(holder); //绑定ViewHolder对象
            return convertView;

    }

    /*存放控件 的ViewHolder*/
    public final class DealViewHolder {
        public TextView fromUserName;
        public TextView money;
        public TextView content;
    }

    public final class  NewsViewHolder {
        public TextView title;
        public TextView content;
        public TextView createTime;
    }
    public final class ReportViewHolder{
        public TextView decription;
    }

}
